//$URL: http://dasding.googlecode.com/svn/trunk/data/dao/couchDB/src/main/java/de/piratech/dasding/data/database/couch/CouchImpl.java $
//$Id: CouchImpl.java 127 2013-04-07 20:43:54Z krieg.maria@gmail.com $
/*
 * Copyright 2012 Piratech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.piratech.dasding.data.database.couch;

import de.piratech.dasding.data.database.ActionCategoryData;
import de.piratech.dasding.data.database.ActionData;
import de.piratech.dasding.data.database.Database;
import de.piratech.dasding.data.database.EventCategoryData;
import de.piratech.dasding.data.database.EventData;
import de.piratech.dasding.data.database.LocationData;
import de.piratech.dasding.data.database.UserData;
import java.net.MalformedURLException;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.ektorp.CouchDbConnector;
import org.ektorp.http.HttpClient;
import org.ektorp.http.StdHttpClient;
import org.ektorp.impl.StdCouchDbInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 */
@Service
public class CouchImpl implements Database {

  private static final Logger LOG = LoggerFactory.getLogger(CouchImpl.class);
  private CouchDbConnector mCouchDBConnecto;
  private UserDataImpl mUserRepo;
  private EventDataImpl mEventRepo;
  private ActionDataImpl mActionRepo;
  private ActionCategoryData mActionCategoryRepo;
  private EventCategoryData mEventCategoryRepo;
  @Value("${database.url}")
  private String mUrl;
  @Value("${database.user}")
  private String mUsername;
  @Value("${database.password}")
  private String mPassword;
  @Value("${database.database}")
  private String mDatabase;

  @PostConstruct
  private void init() throws MalformedURLException {
    LOG.debug("database {}", new Object[] { mDatabase });

    HttpClient authenticatedHttpClient = StringUtils.isNotBlank(mUsername)
        && StringUtils.isNotBlank(mPassword) ? new StdHttpClient.Builder()
        .url(mUrl).username(mUsername).password(mPassword).build()
        : new StdHttpClient.Builder().url(mUrl).build();

    StdCouchDbInstance couchDbInstance = new StdCouchDbInstance(
        authenticatedHttpClient);
    mCouchDBConnecto = couchDbInstance.createConnector(mDatabase, true);
    mUserRepo = new UserDataImpl(mCouchDBConnecto);
    mEventRepo = new EventDataImpl(mCouchDBConnecto);
    mActionRepo = new ActionDataImpl(mCouchDBConnecto);
    mActionCategoryRepo = new ActionCategoryDataImpl(mCouchDBConnecto);
    mEventCategoryRepo = new EventCategoryDataImpl(mCouchDBConnecto);
  }

  @Override
  public UserData getUserData() {
    return mUserRepo;
  }

  @Override
  public EventData getEventData() {
    return mEventRepo;
  }

  @Override
  public ActionData getActionData() {
    return mActionRepo;
  }

  @Override
  public ActionCategoryData getActionCategoryData() {
    return mActionCategoryRepo;
  }

  @Override
  public EventCategoryData getEventCategoryData() {
    return mEventCategoryRepo;
  }

  @Override
  public LocationData getLocationData() {
    return null;
  }
}
